<?php
App::uses('AppController', 'Controller');
/**
 * Expenses Controller
 *
 * @property Expense $Expense
 * @property PaginatorComponent $Paginator
 */
class ExpensesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
        $this->layout='list';
        $model = $this->modelClass;
        $this->Filter->addFilters(
            array(
                'filter1' => array(
                    $model.'.name' => array(
                        'operator' => 'LIKE',
                        'value' => array(
                            'before' => '%', // opcional
                            'after'  => '%'  // opcional
                        )
                    ),
                ),
            )

        );

        $this->Filter->setPaginate('order', $model.'.id DESC')    ; // opcional
        $this->Filter->setPaginate('limit', 10);              // opcional
        // Define conditions
        $this->Filter->setPaginate('conditions', $this->Filter->getConditions());

        $this->$model->recursive = 0;
		$this->set('expenses', $this->Paginator->paginate(array($model.".token_user"=>$this->tokenUser())));
        $mnCadastro = 'active';
        $mnCategory = 'active';
        $this->set(compact('mnCadastro','mnCategory'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
        $this->layout='cadastre';
		if (!$this->Expense->exists($id)) {
            $this->msgNotFould();
            return $this->redirect(array('action' => 'index'));
		}
		$options = array('conditions' => array('Expense.' . $this->Expense->primaryKey => $id));
		$this->set('expense', $this->Expense->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
        $this->layout='cadastre';
        $model = $this->modelClass;
		if ($this->request->is('post')) {
            $this->request->data[$model]['token_user'] = $this->tokenUser();
			$this->Expense->create();
			if ($this->Expense->save($this->request->data)) {
				$this->msgSave();
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->msgSave(false);
			}
		}
		$expenseGroups = $this->Expense->ExpenseGroup->find('list',array('conditions'=>array("ExpenseGroup.token_user"=>$this->tokenUser())));
		$this->set(compact('expenseGroups'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
        $this->layout='cadastre';
		if (!$this->Expense->exists($id)) {
            $this->msgNotFould();
            return $this->redirect(array('action' => 'index'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Expense->save($this->request->data)) {
				$this->msgEdit();
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->msgEdit(false);
			}
		} else {
			$options = array('conditions' => array('Expense.' . $this->Expense->primaryKey => $id));
			$this->request->data = $this->Expense->find('first', $options);
		}
		$expenseGroups = $this->Expense->ExpenseGroup->find('list',array('conditions'=>array("ExpenseGroup.token_user"=>$this->tokenUser())));
		$this->set(compact('expenseGroups'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Expense->id = $id;
		if (!$this->Expense->exists()) {
            $this->msgNotFould();
            return $this->redirect(array('action' => 'index'));
        }
		$this->request->allowMethod('post', 'delete');
		if ($this->Expense->delete()) {
			$this->msgDelete();
		} else {
			$this->msgDelete(false);
		}
		return $this->redirect(array('action' => 'index'));
	}
}
